package cn.maihe.elg.operation.service.business;

import cn.maihe.elg.operation.exception.ErrorCode;
import cn.maihe.elg.operation.exception.SystemRuntimeException;
import cn.maihe.elg.operation.model.vo.business.ListCustomerBlacklistVO;
import cn.maihe.elg.operation.repository.entity.CustomerBlacklistDO;
import cn.maihe.elg.operation.repository.entity.CustomerBlacklistDOCondition;
import cn.maihe.elg.operation.service.BaseDaoService;
import cn.maihe.framework.starter.base.sql.PageBo;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Map;

/**
 * @Description 投标客户黑名单服务
 * @Author WJH
 * @Date 2021/03/25
 */
@Service
public class CustomerBlacklistService extends BaseDaoService<CustomerBlacklistDO> {

    public PageBo<ListCustomerBlacklistVO> selectPageBySql(int pageNo, int pageSize, ListCustomerBlacklistVO vo) {
        Map<String, Object> paramsMap = toMapAndAddParams(vo);
        return this.selectPageBySql("selectCustomerBlacklistList", "selectCustomerBlacklistCount", paramsMap, pageNo, pageSize);
    }

    /**
     * @param creditCode
     * @param centerId                       交易中心添加时必填
     * @param orgId                          机构添加时必填
     * @param excludeSelfCustomerBlacklistId
     * @return
     */
    public boolean existsCreditCode(String creditCode, Long centerId, Long orgId, Long excludeSelfCustomerBlacklistId) {
        Assert.notNull(creditCode, "统一社会信用代码不能为空");

        if(centerId==null && orgId==null){
            throw new SystemRuntimeException(ErrorCode.BSMCM001);
        }
        CustomerBlacklistDOCondition condition = CustomerBlacklistDOCondition.builder().andCustomerCreditCodeEq(creditCode);
        if (centerId != null) {
            condition.andCenterIdEq(centerId);
        }
        if (orgId != null) {
            condition.andOrgIdEq(orgId);
        }

        if (excludeSelfCustomerBlacklistId != null) {
            condition.andIdNe(excludeSelfCustomerBlacklistId);
        }
        return this.exists(condition.build());
    }


    public ListCustomerBlacklistVO getDetailById(Long id) {
        Map<String, Object> paramsMap = Maps.newHashMap();
        paramsMap.put("id", id);
        return this.selectOneBySql("getCustomerBlacklistDetailById", paramsMap);
    }

}
